package com.lineage.william;

import com.lineage.server.datatables.DoorSpawnTable;
import com.lineage.server.model.Instance.L1DoorInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.ArrayList;

public class TowerBlue {
  private final ArrayList<L1PcInstance> _members = new ArrayList<>();
  
  private static TowerBlue _instance;
  
  public static final int STATUS_NONE = 0;
  
  public static final int STATUS_READY = 1;
  
  public static final int STATUS_PLAYING = 2;
  
  public static final int STATUS_CLEANING = 4;
  
  public static final int _minPlayer = 2;
  
  public static final int _maxPlayer = 50;
  
  private int _TowerBlueStatus = 0;
  
  public static TowerBlue getInstance() {
    if (_instance == null)
      _instance = new TowerBlue(); 
    return _instance;
  }
  
  public String enterTowerBlue(L1PcInstance pc) {
    setDoorClose(true);
    if (getInstance().getTowerBlueStatus() == 4) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("抢塔游戏正在重新启动中。"));
      return "";
    } 
    if (getInstance().getTowerBlueStatus() == 2 && !isMember(pc)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1182));
      return "";
    } 
    if (getMembersCount() >= 2 && !isMember(pc)) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("人数已经满了，请等下一场。"));
      return "";
    } 
    if (!pc.getInventory().checkItem(40308, 500000L) && !isMember(pc)) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("金币不足"));
      return "";
    } 
    if (!isMember(pc))
      pc.setTowerIsWhat(2); 
    L1Teleport.teleport(pc, 32862, 32599, (short)511, pc.getHeading(), true);
    addMember(pc);
    setDoorClose(true);
    return "";
  }
  
  private void addMember(L1PcInstance pc) {
    if (!this._members.contains(pc)) {
      this._members.add(pc);
      pc.getInventory().consumeItem(40308, 500000L);
    } 
    if (getMembersCount() == 1 && getTowerBlueStatus() == 0)
      GeneralThreadPool.get().execute(new runTowerBlue()); 
  }
  
  public void endTowerBlue() {
    setTowerBlueStatus(0);
    sendMessage("活动结束，请下次再来");
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      pc.setTowerIsWhat(0);
      L1Teleport.teleport(pc, 33089, 33396, (short)4, 0, true);
      i++;
    } 
    clearMembers();
    setTowerBlueStatus(0);
  }
  
  private void sendMessage(String msg) {
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      if (pc.getMapId() == 511)
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\f3" + msg)); 
      i++;
    } 
  }
  
  private void setTowerBlueStatus(int i) {
    this._TowerBlueStatus = i;
  }
  
  private int getTowerBlueStatus() {
    return this._TowerBlueStatus;
  }
  
  public void clearMembers() {
    this._members.clear();
  }
  
  private void setDoorClose(boolean isClose) {
    L1DoorInstance[] list = DoorSpawnTable.get().getDoorList();
    L1DoorInstance[] array;
    int length = (array = list).length;
    int i = 0;
    while (i < length) {
      L1DoorInstance door = array[i];
      if (door.getMapId() == 511)
        if (isClose) {
          door.close();
        } else {
          door.open();
        }  
      i++;
    } 
  }
  
  public boolean isMember(L1PcInstance pc) {
    return this._members.contains(pc);
  }
  
  public L1PcInstance[] getMembersArray() {
    return this._members.<L1PcInstance>toArray(new L1PcInstance[this._members.size()]);
  }
  
  public int getMembersCount() {
    return this._members.size();
  }
  
  private class runTowerBlue implements Runnable {
    private runTowerBlue() {}
    
    public void run() {
      try {
        TowerBlue.this.setTowerBlueStatus(1);
        Thread.sleep(10000L);
        TowerBlue.this.sendMessage("2分钟后开始抢塔活动，请做好‘组队’准备");
        Thread.sleep(60000L);
        TowerBlue.this.sendMessage("1分钟后开始抢塔活动，请做好‘组队’准备");
        Thread.sleep(50000L);
        TowerBlue.this.sendMessage("倒数10秒");
        Thread.sleep(5000L);
        TowerBlue.this.sendMessage("倒数5秒");
        Thread.sleep(1000L);
        TowerBlue.this.sendMessage("4秒");
        Thread.sleep(1000L);
        TowerBlue.this.sendMessage("3秒");
        Thread.sleep(1000L);
        TowerBlue.this.sendMessage("2秒");
        Thread.sleep(1000L);
        TowerBlue.this.sendMessage("1秒");
        Thread.sleep(1000L);
        World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("斗塔活动已经开始，停止报名。"));
        TowerBlue.this.sendMessage("篮队【" + TowerBlue.this.getMembersCount() + "】人 VS 红队【" + 
            TowerRed.getInstance().getMembersCount() + "】人");
        TowerBlue.this.setDoorClose(false);
        TowerBlue.this.setTowerBlueStatus(2);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } 
    }
  }
}
